package ch.fusun.baron.basic.client.ui.player;

import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class SelectionTable<T> {

	private final TableViewer tableViewer;

	private StructuredSelection selection = null;

	private final ColumnProvider<T>[] columnProviders;

	public SelectionTable(Composite parent, ColumnProvider<T>[] columnProviders) {
		this.columnProviders = columnProviders;
		tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
		Table table = tableViewer.getTable();
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		tableViewer.setContentProvider(ArrayContentProvider.getInstance());
		createColumns(parent, tableViewer);
		tableViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {

					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						selection = (StructuredSelection) event.getSelection();
					}
				});
	}

	private void createColumns(final Composite parent,
			final TableViewer tableViewer) {
		String[] titles = new String[columnProviders.length];
		int[] bounds = new int[columnProviders.length];

		int i = 0;
		TableViewerColumn col;
		for (final ColumnProvider<T> provider : columnProviders) {
			titles[i] = provider.getTitle();
			bounds[i] = provider.getBounds();
			i++;

			// First column is for the first name
			col = createTableViewerColumn(tableViewer, provider.getTitle(),
					provider.getBounds(), i);
			col.setLabelProvider(new ColumnLabelProvider() {
				@SuppressWarnings("unchecked")
				@Override
				public String getText(Object element) {
					return provider.getText((T) element);
				}

				@SuppressWarnings("unchecked")
				@Override
				public Image getImage(Object element) {
					return provider.getImage((T) element);
				}
			});
		}
	}

	/**
	 * @return The selection made
	 */
	public T getSelection() {
		return (T) selection.getFirstElement();
	}

	public void setInput(List<T> elements) {
		tableViewer.setInput(elements);
	}

	private static TableViewerColumn createTableViewerColumn(
			TableViewer viewer, String title, int bound, final int colNumber) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
				SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		column.setWidth(bound);
		column.setResizable(true);
		column.setMoveable(true);
		return viewerColumn;

	}

}
